package com.chj.design.sk;

public class Code14_Singleton {

//	最简单的懒汉式
	public static class Singleton {
		private static Singleton singleton;

		private Singleton() {
		}

		public static Singleton getInstance() {
			if (singleton == null) {
				singleton = new Singleton();
			}
			return singleton;
		}
	}

//	最简单的懒汉式
	public static class Singleton02 {
		private static Singleton02 singleton;

		private Singleton02() {
		}

		public static synchronized Singleton02 getInstance() {
			if (singleton == null) {
				singleton = new Singleton02();
			}
			return singleton;
		}
	}

//	饿汉式
	public static class Singleton03 {
		private static Singleton03 singleton = new Singleton03();

		private Singleton03() {
		}

		public static Singleton03 getInstance() {
			return singleton;
		}
	}

//	静态内部类
	public static class Singleton04 {
		private static class SingletonHolder {
			private static final Singleton04 INSTANCE = new Singleton04();
		}

		private Singleton04() {
		}

		public static final Singleton04 getInstance() {
			return SingletonHolder.INSTANCE;
		}
	}

	// 双重校验锁
	public static class Singleton05 {
		private volatile static Singleton05 singleton;

		private Singleton05() {
		}

		public static Singleton05 getSingleton() {
			if (singleton == null) {
				synchronized (Singleton05.class) {
					if (singleton == null) {
						singleton = new Singleton05();
					}
				}
			}
			return singleton;
		}
	}
}
